home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16168 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [HELP!!!] Segmentation Fault!
  5. Date: Tue, 09 Apr 1996 17:55:13 -0400
  6. Organization: Datalytics, Inc
  7. Message-ID: <316ADCC1.629F@datalytics.com>
  8. References: <4k4gja$mm@nuscc.nus.sg>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Virus wrote:
  16. [snip]
  17. > I keep getting segmentation fault in my program, and I have tried using the
  18. > debugger (gdb in unix), but I cannot understand what the debugger is trying
  19. > to say..[snip]
  20. > char* SpaceStudentFile::readNextField()
  21. > {
  22. >   char str[40];
  23. >   int i = 0;
  24. >   // take away starting whitespaces
  25. >   while (str[0] = fgetc(fd)) {
  26. >         if (*str == ' ') continue;
  27. >         if (*str == '\t') continue;
  28. >         if (*str == '\n') continue;
  29. >         if (*str == EOF) return NULL;
  30. >         break;
  31. >   }
  32. >   // read from " to next "
  33. >   if (str[0] == '"') {
  34. >         for (i=0; ((str[i] = fgetc(fd)) != '"'); i++)
  35. >         ;
  36. >         str[i]=0;
  37. >         return str;
  38. >   }
  39. >   // else read until next whitespace
  40. >   for (i=1; ((str[i] = fgetc(fd)) != ' ')
  41. >         && (str[i] != '\n') && (str[i] != '\t'); i++)
  42. >   ;
  43. >   str[i] = 0;
  44. >   return str;
  45. > }
  46.  
  47. Your problem lies in this function.  You have allocated storage 
  48. on the stack (char str[40]), then return the address of that 
  49. storage.  By the time the function returns, the array on the 
  50. stack no longer exists.  You need persistent storage.
  51.  
  52. One scheme calls for readNextField to allocate memory on the 
  53. heap and return the pointer to that memory.  This introduces an 
  54. ownership problem though, because readNextField allocates but 
  55. doesn't release memory.  readNextField's caller must delete the 
  56. memory it allocated.  This is subject to problems; it is not a 
  57. wise approach.
  58.  
  59. Another scheme calls for allocating storage for readNextField to 
  60. use before calling it, passing a pointer to that storage, and 
  61. letting readNextField populate that storage.  With this 
  62. approach, readNextField must validate the pointer and it must 
  63. assume that there is sufficient space.  This approach is better 
  64. than the first, but it still leaves room for improvement.
  65.  
  66. The other scheme calls for a class to manage the storage, 
  67. returning an object of that class from readNextField.  That is 
  68. the role of a string class.  This approach may be beyond what 
  69. you're ready to do, however.
  70.  
  71. -- 
  72. Robert Stewart        | My opinions are usually my own.
  73. Datalytics, Inc.    | stew@datalytics.com
  74.